import { getDisease, deleteDisease } from '../../../../../utils/api/medicalCondition'
import { getPatientBaseObject } from '../../../../../utils/api/patientBase'
import userBaseInfoState from '../../../../../stores/userBaseInfo'
// pages/home/home-nav/medical-condition/disease-info/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: <PatientBaseEntity>{
      patientUid: '',
      wxUid: '',
      physicalStrength: '',
    },
    user: {
      PCS: '1分（良好）',
      diseases: [{
        pddid: 123,
        name: '高血压',
        year: 1,
        month: 2,
      }, {
        pddid: 123,
        name: '糖尿病',
        year: 2,
        month: 1,
      }, {
        pddid: 123,
        name: '脂肪肝',
        year: 1,
        month: 0,
      }, {
        pddid: 123,
        name: '风湿',
        year: 0,
        month: 1,
      }]
    }
  },

  updateUserInfo() {
    const data = <PatientBaseEntity>{ wxUid: wx.getStorageSync("openid") };
    // 如果已经获取状态信息那么无需再次获取，判断是否具有 patientUid和username即可确认
    getPatientBaseObject(data).then((res: any) => {
      console.log('获取基本信息成功-home-nav', res);
      userBaseInfoState.onChangeData(res.data[0])
      this.setData({
        userInfo: userBaseInfoState.data
      })
      this.updatePCS();
      this.updateDisease();
    }).catch((err: any) => {
      console.log("获取基本信息失败", err);
    })
  },

  updatePCS() {
    // console.log('获取到的PCS是', this.data.userInfo.physicalStrength);
    if (this.data.userInfo.physicalStrength === null) {
      this.setData({
        'user.PCS': '暂无数据，请添加'
      })
    }
    if (this.data.userInfo.physicalStrength === 0) {
      this.setData({
        'user.PCS': '0分（优秀）'
      });
      // console.log('现在的PCS是', this.data.user.PCS);
    }
    if (this.data.userInfo.physicalStrength === 1) {
      this.setData({
        'user.PCS': '1分（良好）'
      });
      // console.log('现在的PCS是', this.data.user.PCS);
    }
    if (this.data.userInfo.physicalStrength === 2) {
      this.setData({
        'user.PCS': '2分（一般）'
      });
      // console.log('现在的PCS是', this.data.user.PCS);
    }
    if (this.data.userInfo.physicalStrength === 3) {
      this.setData({
        'user.PCS': '3分（较差）'
      });
      // console.log('现在的PCS是', this.data.user.PCS);
    }
    if (this.data.userInfo.physicalStrength === 4) {
      this.setData({
        'user.PCS': '4分（极差）'
      });
      // console.log('现在的PCS是', this.data.user.PCS);
    }
  },

  updateDisease() {
    let patientUid = this.data.userInfo.patientUid as string
    getDisease(patientUid).then((res: any) => {
      if (res.code === 0 && res.data.length !== 0) {
        this.data.user.diseases = res.data.map((item: any) => ({
          pddid: item["id: "],
          name: item["disease_name: "],
          year: item["year: "],
          month: item["month: "],
        }));
        this.setData({
          'user.diseases': this.data.user.diseases,
        })
      } else {
        console.error("Failed to update disease information", res.msg);
      }
    }).catch((err: any) => {
      console.error("API call failed", err);
    });
  },

  removeDisease(e: any) {
    const index = e.currentTarget.dataset.index;
    const pddId = this.data.user.diseases[index].pddid
    // console.log(index);
    // console.log(pddId);
    deleteDisease(pddId).then((res: any) => {
      // console.log('删除药品成功', res);
      this.updateUserInfo();
    }).catch((err: any) => {
      console.log('删除药品失败', err);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.updateUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.updateUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})